package com.example.administrator.student4;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.student4.Adapter.SubjectAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * Created by Administrator on 2016/12/12.
 */
public class Subject extends Activity {
    ListView lv;
    String path = "http://10.0.2.2:8080/andorid/subject?request=";
    HttpURLConnection connection;
    JSONArray array;
    TextView add;
    MyHandler myHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.subject);
        lv = (ListView) findViewById(R.id.listView);
        add = (TextView) findViewById(R.id.add);
        myHandler = new MyHandler();
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    URL url = new URL(path+"findall");
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("POST");
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    connection.connect();
                    if(connection.getResponseCode()==200){
                        InputStream is = connection.getInputStream();
                        array = WebUtil.ioToJsonArray(is);
                        myHandler.sendEmptyMessage(0x111);
                    }
                } catch (java.io.IOException | JSONException e) {
                    e.printStackTrace();
                }
                finally {
                    connection.disconnect();
                }
            }
        }.start();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(Subject.this,SubjectDetail.class);
                try {
                    intent.putExtra("subject",array.getJSONObject(position).getString("subject"));
                    intent.putExtra("teacher",array.getJSONObject(position).getString("teacher"));
                    intent.putExtra("term",array.getJSONObject(position).getString("term"));
                    intent.putExtra("id",array.getJSONObject(position).getInt("id"));
                    intent.putExtra("becon",1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                startActivity(intent);
            }
        });
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Subject.this,SubjectDetail.class);
                intent.putExtra("becon",2);
                startActivity(intent);

            }
        });
    }
    class MyHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 0x111){
                SubjectAdapter adapter = new SubjectAdapter(Subject.this,array);
                lv.setAdapter(adapter);
            }
        }
    }
}
